package com.tyqx.hashchainbiz.controller.jzt;

import com.tyqx.hashchainbean.module.JZTGS1Relationship;
import com.tyqx.hashchainbean.module.JZTLogisticsRelationship;
import com.tyqx.hashchainbean.vo.JZTGS1RelationshipVO;
import com.tyqx.hashchainbiz.service.JZTGS1RelationshipService;
import com.tyqx.hashchainbiz.service.impl.ExcelService;
import hashchainutil.common.Dto;
import hashchainutil.common.DtoUtil;
import hashchainutil.common.EmptyUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/JZTApi/GS1")
@CrossOrigin
public class JZTGS1RelationshipController {

    @Resource
    private ExcelService excelService;

    @Resource
    private JZTGS1RelationshipService jztgs1RelationshipService;

    @RequestMapping(value="/addJZTGS1Relationship", method = RequestMethod.POST)
    @ResponseBody
    public Dto addJZTGS1Relationship(@RequestBody JZTGS1Relationship jztgs1Relationship) throws Exception {

        String msg="";
        Integer flag= jztgs1RelationshipService.addJZTGS1Relationship(jztgs1Relationship);
        if(flag>0){
            msg="添加GS1关系成功";
        }else{
            msg="添加GS1关系失败";
        }
        return DtoUtil.returnSuccess(msg);
    }

    @RequestMapping(value="/addJZTGS1RelationshipUploadExcel", method = RequestMethod.POST)
    @ResponseBody
    public Dto addJZTCodeCirculationRelationshipUploadExcel(HttpServletRequest request) {
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;

        MultipartFile file = multipartRequest.getFile("fileName");
        if (file.isEmpty()) {
            return DtoUtil.returnFail("文件不能为空","100001");
        }
        String msg="";
        try {
            InputStream inputStream = file.getInputStream();
            List<List<Object>> list = excelService.getBankListByExcel(inputStream, file.getOriginalFilename());
            inputStream.close();
            Integer flag=0;

            Date currentDate = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date createTime = sdf.parse(sdf.format(currentDate));
            Date updateTime = createTime;

            List<JZTGS1Relationship> jztgs1RelationshipList=new ArrayList<>();
            for (int i = 0; i < list.size(); i++) {
                List<Object> lo = list.get(i);
                JZTGS1Relationship jztgs1Relationship=new JZTGS1Relationship();
                for (int j=0;j<lo.size();j++){
                    if(j==0){
                        jztgs1Relationship.setSsccCode(lo.get(j).toString());
                    }else if(j==1){
                        jztgs1Relationship.setTraceabilityCode(lo.get(j).toString());
                    }
                }
                if(EmptyUtils.isNotEmpty(jztgs1Relationship.getSsccCode())){

                    jztgs1Relationship.setCreateTime(createTime);
                    jztgs1Relationship.setUpdateTime(updateTime);
                    jztgs1RelationshipList.add(jztgs1Relationship);
                }
            }
            int size=jztgs1RelationshipList.size();
            if(size>0){
                Integer flag1=jztgs1RelationshipService.insertBatch(jztgs1RelationshipList);
                if(flag1>0){
                    msg="添加GS1关系成功";
                    return DtoUtil.returnSuccess(msg);
                }else{
                    msg="添加GS1关系失败";
                    return DtoUtil.returnFail(msg, "10001");
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            msg="文件解析失败或者添加追溯信息错误";
        }


        return DtoUtil.returnSuccess(msg);
    }


    @RequestMapping(value="/addJZTGS1RelationshipBatch", method = RequestMethod.POST)
    @ResponseBody
    public Dto addJZTGS1RelationshipBatch(@RequestBody List<JZTGS1Relationship> jztgs1RelationshipList) throws Exception {
        String msg="";
        Date currentDate = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date createTime = sdf.parse(sdf.format(currentDate));
        Date updateTime = createTime;
        for (int i=0;i<jztgs1RelationshipList.size();i++){


            JZTGS1Relationship jztgs1Relationship=jztgs1RelationshipList.get(i);
            jztgs1Relationship.setUpdateTime(updateTime);
            jztgs1Relationship.setCreateTime(createTime);
        }
        Integer flag=jztgs1RelationshipService.insertBatch(jztgs1RelationshipList);

        if(flag>0){
            msg="添加GS1关系成功";
            return DtoUtil.returnSuccess(msg);
        }else{
            msg="添加GS1关系失败";
            return DtoUtil.returnFail(msg, "10001");
        }
    }

    @RequestMapping(value="/getJZTGS1RelationshipBySsccCode", method = RequestMethod.POST)
    @ResponseBody
    public Dto getJZTGS1RelationshipBySsccCode(@RequestParam(value = "ssccCode",required = true) String ssccCode) throws Exception {
        String msg="";

        try {
            List<JZTGS1RelationshipVO> jztgs1RelationshipVOList=jztgs1RelationshipService.getJZTGS1RelationshipListBySSccCode(ssccCode);

            if(jztgs1RelationshipVOList.size()>0){
                msg="查询GS1关系成功";
                return DtoUtil.returnSuccess(msg,jztgs1RelationshipVOList);
            }else{
                msg="未查询到记录";
                return DtoUtil.returnFail(msg, "10001");
            }
        }catch (Exception e){
            msg="查询GS1关系失败";
            return DtoUtil.returnFail(msg, "10001");
        }
    }
}
